package com.common.util;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author: stefanie-zy
 * @time: 2024/1/29-14:56
 * @description: 文件工具类
 */
@Schema(name = "工具-文件")
@Component
@Slf4j
public class FileUtil {

    /**
     * 校验文件路径是否存在，不存在则生成
     * @param path 目标路径
     */
    public static void checkAndCreateDirectory(String path) {
        Path p = Paths.get(path);
        if (!Files.exists(p)) {
            try {
                Files.createDirectories(p);
                log.info("file directory {} is created successfully", path);
            } catch (IOException e) {
                log.error("failed to create the {} file directory, and the error message is {}", path, e.getMessage());
                e.printStackTrace();
            }
        }
    }
}
